package com.knockim.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.knockim.R;
import com.knockim.cache.DrugCache;
import com.knockim.model.Drug;
import com.knockim.view.vicinity.DrugProfileActivity;

public class MainTabFavoriteActivity extends Activity {
	
	/**
     * 收藏产品列表
     */
    private ListView mListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tab_favorite);
		
		initView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_tab_favorite, menu);
		return true;
	}

	private void initView() {
		mListView = (ListView) findViewById(R.id.favorite_list);
		mListView.setAdapter(new VicinityListAdapter(this, getFavoriteDrugList()));
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ImageView avatar = (ImageView) view.findViewById(R.id.avatar);
                Drug drug = (Drug) avatar.getTag();
                if (drug != null) {
                    Intent intent = new Intent(MainTabFavoriteActivity.this, DrugProfileActivity.class);
                    intent.putExtra("code", drug.getCode());
                    startActivity(intent);
                }
            }
        });
	}
	
	private List<Drug> getFavoriteDrugList(){
		List<Drug> favoriteDrugList = new ArrayList<Drug>();
		List<Drug> drugsCache = DrugCache.getDrugListCache();
		for(Drug drug : drugsCache){
			if(drug.isFavorite()){
				favoriteDrugList.add(drug);
			}
		}
		return favoriteDrugList;
	}
	
	public class VicinityListAdapter extends BaseAdapter {

		private List<Drug> drugList;
		private LayoutInflater mInflater;
		
		public VicinityListAdapter(Context context, List<Drug> drugList){
			this.drugList = drugList;
			 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public int getCount() {
			if(drugList != null){
				return drugList.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			if (drugList != null) {
                return drugList.get(position);
            }
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.vicinity_list_item, null);
                viewHolder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
                viewHolder.name = (TextView) convertView.findViewById(R.id.name);
                viewHolder.distance = (TextView) convertView.findViewById(R.id.distance);
                viewHolder.oldPrice = (TextView) convertView.findViewById(R.id.oldprice);
                viewHolder.retailprice = (TextView) convertView.findViewById(R.id.retailprice);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            
            Drug drug = drugList.get(position);
            
            viewHolder.avatar.setTag(drug);            
            viewHolder.name.setText(drug.getName());
            viewHolder.distance.setText(getDistanceText(drug.getDistance()));
            //viewHolder.oldPrice.setText(String.valueOf(drug.getOldPrice()));
            viewHolder.retailprice.setText("¥" + String.valueOf(drug.getRetailPrice()));
            
            return convertView;
		}
		
		private String getDistanceText(long distance) {
			
			return distance * 100 + "米以内";
		}
		
		private class ViewHolder {
            ImageView avatar;
            TextView name;
            TextView distance;
            TextView oldPrice;
            TextView retailprice;
        }
		
	}
	
}
